package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AbolishappFormMap;
import com.iemes.entity.EquipCategoryFormMap;
import com.iemes.entity.EquipFileFormMap;
import com.iemes.entity.EquipSpareFormMap;
import com.iemes.entity.RepairappFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.AbolishappMapper;
import com.iemes.mapper.equip.EquipCategoryMapper;
import com.iemes.mapper.equip.EquipFileMapper;
import com.iemes.mapper.equip.RepairappMapper;
import com.iemes.mapper.equip.SpareMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping(value="equip")
@Controller
public class EquipFileController extends BaseController{
	@Inject
	private EquipFileMapper fileMapper;	
	@Inject
	private EquipCategoryMapper categorymapper;	
	@Inject
	private RepairappMapper repaintManager;	
	@Inject
	private AbolishappMapper abolishapp;
	@Inject
	private SpareMapper spareMapper;
	
	@RequestMapping(value = "equip_filetance", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		//种类表信息
		List<EquipCategoryFormMap> categoryFormMap = categorymapper.findByWhere(new EquipCategoryFormMap());
		List<EquipFileFormMap> findByWhere = fileMapper
				.findByWhere(new EquipFileFormMap());
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("categoryFormMap", categoryFormMap);
		model.addAttribute("equipbom", findByWhere);
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/file/equipbom_list";		
	}
	
	@ResponseBody
	@RequestMapping("equip_fileByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);
		if (equipFileFormMap.get("status")==null) {
			equipFileFormMap.put("status", 1);
		}
		List<Map<String, Object>> equipFiles = fileMapper.getEquipFiles(equipFileFormMap);			
		try {
			LuogicPaging(pageNow, pageSize, equipFiles);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return pageView;				
	}
		
	@ResponseBody
	@RequestMapping(value = "file_deleteEntity")
	@SystemLog(module = "设备台账单", methods = "设备-删除台账单")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			equipFileFormMap.put("status", 2);
			equipFileFormMap.put("id", str[i]);
			equipFileFormMap.put("date", sdf.format(date));
			equipFileFormMap.put("fillPeopleId", userFormMap.get("id"));
			fileMapper.editEntity(equipFileFormMap);
		}
		return "success";
	}
	@RequestMapping("file_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		EquipSpareFormMap equipSpareFormMap = getFormMap(EquipSpareFormMap.class);
		equipSpareFormMap.put("equipmentFileId", id);
		List<EquipSpareFormMap> findByPage = spareMapper.findByWhere(equipSpareFormMap);
		List<EquipCategoryFormMap> categoryFormMap = categorymapper.findByWhere(new EquipCategoryFormMap());		
		EquipFileFormMap equipfile = fileMapper.findbyFrist("id", id,
				EquipFileFormMap.class);
		model.addAttribute("equipfile", equipfile);
		model.addAttribute("categoryFormMap", categoryFormMap);
		model.addAttribute("findByPage", findByPage);
		return Common.BACKGROUND_PATH + "/equip/file/equipbom_edit";
	}

	@ResponseBody
	@Transactional(readOnly=false)
	@RequestMapping(value = "file_editEntity")
	@SystemLog(module = "设备台账单", methods = "台账单-修改台账单")// 凡需要处理业务逻辑的.都需要记录操作日	
	public String editEntity() throws Exception {
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);	
		EquipSpareFormMap equipSpareFormMap = getFormMap(EquipSpareFormMap.class);
		spareMapper.deleteByAttribute("equipmentFileId", equipFileFormMap.get("id").toString(), EquipSpareFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		equipFileFormMap.put("fillPeopleId", userFormMap.get("id"));
		equipFileFormMap.put("date", sdf.format(date));
		String[] sparepartName=request.getParameterValues("sparepartName");
		String[] sparepartModel=request.getParameterValues("sparepartModel");
		String[] sparepartSpecification=request.getParameterValues("sparepartSpecification");
		String[] sparepartFactory=request.getParameterValues("sparepartFactory");
		String[] factoryTelephone=request.getParameterValues("factoryTelephone");
		String[] factoryAddress=request.getParameterValues("factoryAddress");
		for (int i = 0; i < sparepartName.length; i++) {
			if (sparepartName[i].isEmpty() && sparepartModel[i].isEmpty() && sparepartSpecification[i].isEmpty()
				&& sparepartFactory[i].isEmpty() && factoryTelephone[i].isEmpty() && factoryAddress[i].isEmpty()	
					) {
				continue;
			}
			equipSpareFormMap.set("equipmentFileId", equipFileFormMap.get("id"));
			equipSpareFormMap.set("sparepartName", sparepartName[i]);
			equipSpareFormMap.set("sparepartModel", sparepartModel[i]);
			equipSpareFormMap.set("sparepartSpecification", sparepartSpecification[i]);
			equipSpareFormMap.set("sparepartFactory", sparepartFactory[i]);
			equipSpareFormMap.set("factoryTelephone", factoryTelephone[i]);
			equipSpareFormMap.set("factoryAddress", factoryAddress[i]);
			spareMapper.addEntity(equipSpareFormMap);
			equipSpareFormMap.clear();
		}		
		fileMapper.editEntity(equipFileFormMap);
		return "success";
	}
	
	@ResponseBody
	@Transactional(readOnly=false)
	@RequestMapping(value = "file_maintenEntity")
	@SystemLog(module = "设备台账单", methods = "设备-添加维修台账单")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String maintenEntity() throws Exception {
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);
		RepairappFormMap repairappFormMap = getFormMap(RepairappFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for (int i = 0; i < str.length; i++) {
			repairappFormMap.put("fileId", str[i]);
			repairappFormMap.set("applyPeopleId", userFormMap.get("id"));
			repairappFormMap.set("date", sdf.format(date));
			repairappFormMap.set("status", 1);
			equipFileFormMap.set("equipmentStatus", 2);
			equipFileFormMap.put("id", str[i]);
			equipFileFormMap.put("date", sdf.format(date));	
			//生成设备维修
			repaintManager.addEntity(repairappFormMap);
			repairappFormMap.clear();
			//修改设备台账（设备状态）
			fileMapper.editEntity(equipFileFormMap);

		}

		return "success";
	}	
		
	@ResponseBody
	@Transactional(readOnly=false)
	@RequestMapping(value = "equip_filescrapEntity")
	@SystemLog(module = "设备台账单", methods = "设备-报废设备")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String filescrapEntity() throws Exception {
		AbolishappFormMap abolishappFormMap = getFormMap(AbolishappFormMap.class);
		EquipFileFormMap equipFileFormMap = getFormMap(EquipFileFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
						.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);		
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		for (int i = 0; i < str.length; i++) {
			equipFileFormMap.put("equipmentStatus", 3);
			equipFileFormMap.put("id", str[i]);			
			equipFileFormMap.put("date", sdf.format(date));			
			fileMapper.editEntity(equipFileFormMap);
			//向报废表插入数据
			abolishappFormMap.put("date", sdf.format(date));
			abolishappFormMap.put("equipmentFileId", str[i]);
			abolishappFormMap.put("fillPeopleId", userFormMap.get("id"));
			abolishappFormMap.put("status",1);
			abolishapp.addEntity(abolishappFormMap);
			abolishappFormMap.clear();			
		}
		return "success";
	}
	
}
